import java.util.ArrayList;
import java.util.Collections;


public class Ex7_2 {

	/*
	https://class.coursera.org/bioinformatics-001/forum/thread?thread_id=812

	 */
	
	public String resultV, resultW;
	public Integer maxLastColScore, indexMaxLastCol;
	
	public Ex7_2() {
		resultV = "";
		resultW = "";
		indexMaxLastCol = -1;
		maxLastColScore = 0;
		
	}


	public static void main(String[] args) {
		Ex7_2 exer = new Ex7_2();
		ArrayList<String> inputs = Utils.readLines("C:\\Users\\josemfer\\Downloads\\dataset_77_5.txt");		
		String v = inputs.get(0);
		String w = inputs.get(1);
		ArrayList<Integer[][]> results = exer.getBacktrack(v, w);
		Integer[][] backt = results.get(0);
		Integer[][] scores = results.get(1);			
		
		exer.getOutputLCS(scores,backt,v,w,exer.indexMaxLastCol,w.length());
		Utils.writeFile( exer.maxLastColScore + "\r\n"   
				+ exer.resultV + "\r\n" 
				+ exer.resultW, 
				"C:\\Users\\josemfer\\Downloads\\out_7_2.txt");
	}

	
	
	private void getOutputLCS(Integer[][] scores, Integer[][] backtrack, 
			String v, String w, Integer i, Integer j) {
		//System.out.println("i=" + i + "   j=" + j);
		if (i == 0 || j == 0 ) {
			return;
		}
		if (backtrack[i][j] == 2) { // '2' = down
			getOutputLCS(scores,backtrack,v,w,i-1,j);			
			resultV += v.charAt(i-1);
			resultW += "-";

		} else if (backtrack[i][j] == 1) {   // '1' = right 
			getOutputLCS(scores,backtrack,v,w,i,j-1);
			resultV += "-";
			resultW += w.charAt(j-1);

		} else if (backtrack[i][j] == 0) {
			getOutputLCS(scores, backtrack,v,w,i-1,j-1);   //  '0' = diagonal
			resultV += v.charAt(i-1);
			resultW += w.charAt(j-1);			
		}		
	}	

	/*
	 *   '0' = diag   '1' = right  '2' = down
	 */
	private ArrayList<Integer[][]> getBacktrack(String v, String w) {	
		ArrayList<Integer[][]> results = null;			
		
		Integer n = v.length();
		Integer m = w.length();
		Integer s[][] = new Integer[n+1][m+1];
		Integer back[][] = new Integer[n+1][m+1];		
		for (int i=0;i<=n;i++) {
			s[i][0] = 0;
			back[i][0] = 2; 
		}
		for (int j=0;j<=m;j++) {
			s[0][j] = -j;
			back[0][j] = 2; 
		}
		for (int i=1;i<=n;i++) {
			for (int j=1;j<=m;j++) {
				// calculate down and right
				Integer down = s[i-1][j] - 1;
				Integer right = s[i][j-1] - 1;
				// calculate diag				
				char vi = v.charAt(i-1);
				char wj = w.charAt(j-1);
				//Integer blosum_i = pam250.indexOf(vi);
				//Integer blosum_j = pam250.indexOf(wj);
				Integer diag = s[i-1][j-1];
				if (vi == wj) {
					diag += 1;
				} else {
					diag -= 1;
				}				
				
				//save score and backtrack matrix			
				Integer score = Utils.max(new Integer[]{diag, right, down});			
				s[i][j] = score;
				if (  j == w.length() && score > this.maxLastColScore) {
					this.maxLastColScore = score;
					this.indexMaxLastCol = i;				
				}								
				Integer ind = Utils.maxIndex(new Integer[]{diag, right, down});					
				back[i][j] = ind;	
				
			}				
		}
		results = new ArrayList<Integer[][]>();		
		results.add(back);		
		results.add(s);		
		return results;
	}


}
